package Leetcode.搜索与回溯;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author kirito
 * @Date 2023/10/23 9:45
 * @PackageName:Leetcode.搜索与回溯
 * @ClassName: 电话号码的字母组合
 * @Description:
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 *
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 *
 * 示例 1：
 *
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * 示例 2：
 *
 * 输入：digits = ""
 * 输出：[]
 * 示例 3：
 *
 * 输入：digits = "2"
 * 输出：["a","b","c"]
 * @Version 1.0
 */
public class 电话号码的字母组合 {
    List<String> ans;
    Map<Character,String> map;
    public List<String> letterCombinations(String digits) {
        this.ans = new ArrayList<>();
        if(digits.length() == 0) return ans;

        this.map = new HashMap<>();
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");

        dfs(digits,0,new StringBuilder());

        return ans;
    }

    public void dfs(String digits,int index,StringBuilder builder){
        if(index == digits.length()){
            ans.add(builder.toString());
        }else{
            char c = digits.charAt(index);
            String str = map.get(c);
            int count = str.length();
            for(int i = 0;i < count; ++i){
                builder.append(str.charAt(i));
                dfs(digits,index + 1,builder);
                builder.deleteCharAt(index);
            }
        }

    }
}
